/**
 * 初始化资源的脚本
 * @author rxliuli
 */

/**
 * 表单验证插件
 */
;(function initVeeValidate() {
    const config = {
        locale: 'zh_CN',
        delay: 0,
        fieldsBagName: 'fieldBags',
        events: 'input|change|blur',
    }
    const dict = {
        zh_CN: {
            messages: {
                required: () => '输入不能为空',
                alpha_num: () => '输入只能为英文或数字',
                ip: () => '请输入有效的IP地址',
                numeric: () => '请正确输入数字',
                digits: (filed, num) => `必须是数字,且精确到${num}位数`,
                max: (filed, num) => `不能超过${num}个字符`,
                max_value: (filed, num) => `值必须小于${num}`,
                url: () => '不是一个有效的url',
                min_value: (filed, num) => `值必须大于${num}`,
                email: () => `请输入有效的邮箱`,
            },
        },
    }
    Vue.use(VeeValidate, config)
    VeeValidate.Validator.localize(dict)

    const domainReg = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
    VeeValidate.Validator.extend('domain', {
        getMessage: field => '请输入正确的域名，示例：zx.com',
        validate: value => domainReg.test(value)
    });

    const telephoneReg =  /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/;
    VeeValidate.Validator.extend('tel', {
        getMessage: field => '[固定电话] 格式错误，示例: [010-88888888]',
        validate: value => telephoneReg.test(value)
    });

    const mobileReg = /^1[3|4|5|7|8][0-9]{9}$/;
    VeeValidate.Validator.extend('mobile', {
        getMessage: field => '[移动电话] 格式错误，示例: [13532611510]',
        validate: value => mobileReg.test(value)
    });

    const zipCodeReg = /^\d{6}$/;
    VeeValidate.Validator.extend('zipCode', {
        getMessage: field => '[邮编编号] 格式错误。示例: 510000',
        validate: value => zipCodeReg.test(value)
    });
})()

/**
 * 页面进入自动自适应窗口大小
 */
;(function resizeForWindow() {
    const windowHeight = document.documentElement.clientHeight - 200
    const element = document.querySelector('.right');
    if(!element)return;
    const contentHeight = element.clientHeight
    document.querySelector('.main').style.minHeight = Math.max(windowHeight, contentHeight) + 'px'
})()

;(function initCommonMixins(){
    Vue.mixin({
        methods:{
            _forward(view,params){
                forward(view,params);
            }
        }
    })
})()

//不断监听窗口高度是否改变并作出响应，默认每 100ms 一次
rx.watch(() => {
        const windowHeight = $(window).height() - 200
        const contentHeight = $('.right').height()
        return Math.max(windowHeight, contentHeight)
    }, (val) => $('.main').css('min-height', val),
)

